from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.pagination import PageNumberPagination
from ctbloge.apps.cms.models import Category
from .serializers import *


class ResourcePagination(PageNumberPagination):
    page_size = 12
    page_size_query_param = 'page_size'
    max_page_size = 100

    def get_paginated_response(self, data):
        return Response({
            'count': self.page.paginator.count,  # 总数量
            'page': self.page.number,  # 当前页码
            'page_size': self.page_size,  # 每页数量
            'total_pages': self.page.paginator.num_pages,  # 总页数
            'next': self.get_next_link(),  # 下一页链接
            'previous': self.get_previous_link(),  # 上一页链接
            'results': data  # 当前页返回的数据
        })


class ResourceDownloadViewSet(viewsets.ModelViewSet):
    serializer_class = ResourceSerializer
    queryset = Resource.objects.filter(is_public=True).order_by('-created_date')
    pagination_class = ResourcePagination

    def get_queryset(self):
        queryset = Resource.objects.filter(is_public=True)
        category_id = self.request.query_params.get('category')
        if category_id:
            queryset = queryset.filter(category_id=category_id)
        return queryset

    def retrieve(self, request, *args, **kwargs):
        instance = self.get_object()
        instance.download_count += 1
        instance.save()
        response = super().retrieve(request, *args, **kwargs)
        return response
    #获取所有的资源分类,并返回分类的id和名称
    @action(methods=['get'], detail=False, url_path='categories')
    def categories(self, request):
        categories = Category.objects.filter(resources__isnull=False).distinct()#获取所有有资源的分类
        serializer = CategorySerializer(categories, many=True)
        return Response(serializer.data)

class ResourceNavigateViewSet(viewsets.ModelViewSet):
    serializer_class = FavSiteSerializer
    queryset = FavSite.objects.all()
    def get_queryset(self):
        return FavSite.objects.filter(is_public=True)
    #获取所有的资源分类,并返回分类的id和名称
    @action(methods=['get'], detail=False, url_path='categories')
    def categories(self, request):
        categories = Category.objects.filter(fav_sites__isnull=False).distinct()#获取所有有资源的分类
        serializer = CategorySerializer(categories, many=True)
        return Response(serializer.data)
class ResourceToolsViewSet(viewsets.ModelViewSet):
    serializer_class = ToolSerializer
    queryset = Tool.objects.all()
    pagination_class = ResourcePagination

    def get_queryset(self):
        queryset = Tool.objects.all()
        category_id = self.request.query_params.get('category')
        if category_id:
            queryset = queryset.filter(category_id=category_id)
        return queryset


    #获取所有的资源分类,并返回分类的id和名称
    @action(methods=['get'], detail=False, url_path='categories')
    def categories(self, request):
        categories = Category.objects.filter(tools__isnull=False).distinct()#获取所有有资源的分类
        serializer = CategorySerializer(categories, many=True)
        return Response(serializer.data)
